﻿if not ArmorCraft_revision then ArmorCraft_revision = {} end
ArmorCraft_revision.options	= ("$Revision: 109 $"):match("(%d+)")

--[[

	ArmorCraftOptions.lua
	=====================

	Rev: $Id: ArmorCraftOptions.lua 109 2008-10-19 20:16:35Z LaoTseu.Ace $

	This file contains the functions to initialise and manage the ArmorCraft options.

	Credit: Thanks to Cladhaire. All the technics used in this file are lifted from TomTom.

]]--

-------------------------------------------------
-- Make sure the ArmorCraft global object exists
-------------------------------------------------

if not ArmorCraft then ArmorCraft = {} end

-------------------------------------------------
-- Local Definitions
-------------------------------------------------

local function print(...) ArmorCraft:print(...) end
local function debug(...) ArmorCraft:debug(...) end

local function sorted_index(table)
    local index = {}
    for key in pairs(table) do tinsert(index,key) end
    sort(index)
    return index
end

local list_of_formulas = {}
local list_of_formula_to_use_keys = {}
local list_of_formula_keys = {}

-- #################################################################################
-- #################################################################################
-- ##
-- ## createconfig()
-- ## --------------
-- ##
-- ## Return a local table that will be used to feed the option dialog functions
-- ##
-- #################################################################################

local function createconfig()
	local options = {}

	options.type = "group"
	options.name = "ArmorCraft"

	options.args = {}

	options.args.main = {
		type = "group",
		name = "About",
		desc = "Be a better crafter/looter",
		order = 10,

		args = {
			descmain1 = {
				type = "description",
				order = 1,
				name = format("Revision: %d", ArmorCraft.revision)
			},
			descmain2 = {
				type = "header",
				order = 2,
				name = "",
			},
			descmain3 = {
				type = "description",
				order = 3,
				name = '"Be a better crafter and looter"',
			},
			descmain4 = {
				type = "description",
				order = 4,
				name = "ArmorCraft allows you to compare an item in your bag, bank, from the loot box, the quest rewards or trade skill window with what the party members or your characters are wearing. It can also be used to find out what are the best things that can be crafted for a party member or for your characters.",
			},
			descmain5 = {
				type = "description",
				order = 5,
				name = "In order to propose better gear, ArmorCraft uses formula to compare the stats of the curent item with the possible replacment. Use SHIFT-LEFT-CLICK on items or the I and AC buttons in the trade skill window to invoke ArmorCraft.",
			},
			descmain6 = {
				type = "description",
				order = 6,
				name = "To display the configuration dialog, use /ac config.",
			},
		},
	}

	local function getfu(info)
		local key,opt
		if info.arg then
			key,opt = string.split(".", info.arg)
		else
			key,opt = info[#info],"#info"
		end
		--print(format("get key=%s, opt=%s", key, opt))

		return ArmorCraft_Formula_to_Use[list_of_formula_to_use_keys[key]][DEFAULT]

	end

	local function setfu(info, arg1, arg2, arg3, arg4)
		local key,opt
		if info.arg then
			key,opt = string.split(".", info.arg)
		else
			key,opt = info[#info],"#info"
		end
		--print(format("set key=%s,opt=%s,arg1=%s", key, opt, arg1 or 'nil'))

		ArmorCraft_Formula_to_Use[list_of_formula_to_use_keys[key]][DEFAULT] = arg1

		-- Clear the cache
		ArmorCraft.cache = {}

	end

	options.args.formulas_to_use = {
		type = "group",
		name = "Formulas to use",
		desc = "Group description 2",
		order = 20,
		get = getfu,
		set = setfu,

		args = {
			fu_descmain1 = {
				type = "description",
				order = 1,
				name = "Select the formula to use for each class.",
			},
		},
	}

	-- Initialise the formula to use list
	assert(ArmorCraft_Formula_to_Use)
	for index, class in ipairs(sorted_index(ArmorCraft_Formula_to_Use)) do

		local okey = 'class' .. index
		list_of_formula_to_use_keys[okey] = class

		options.args.formulas_to_use.args[okey] = {}
		options.args.formulas_to_use.args[okey].name		= class
		options.args.formulas_to_use.args[okey].type 	= "select"
		options.args.formulas_to_use.args[okey].values	= list_of_formulas
		options.args.formulas_to_use.args[okey].desc 	= "Default formula for " .. class
		options.args.formulas_to_use.args[okey].order 	= index +10
	end

	local function getform(info)
		local key,opt
		if info.arg then
			key,opt = string.split(".", info.arg)
		else
			key,opt = info[#info],"#info"
		end
		--print(format("getform key=%s, opt=%s", key, opt))

		return ArmorCraft_Formulas[list_of_formula_keys[key]]
	end

	local function setform(info, arg1, arg2, arg3, arg4)
		local key,opt
		if info.arg then
			key,opt = string.split(".", info.arg)
		else
			key,opt = info[#info],"#info"
		end
		--print(format("setform key=%s,opt=%s,arg1=%s", key, opt, arg1 or 'nil'))

		ArmorCraft_Formulas[list_of_formula_keys[key]] = arg1

		-- Clear the cache
		ArmorCraft.cache = {}
	end

	options.args.formulas = {
		type = "group",
		name = "Edit Formulas",
		desc = "Group description 3",
		order = 30,
		get = getform,
		set = setform,

		args = {
			f_descmain1 = {
				type = "description",
				order = 1,
				name = "Edit the text of the fomulas using ItemBonusLib variables.",
			},
			f_descmain2 = {
				type = "description",
				order = 2,
				name = "See <http://www.wowace.com/wiki/ItemBonusLib-1.0/Bonus> for information about the available variables.",
			},
		},
	}

	-- Initialise the formula list
	assert(ArmorCraft_Formulas)
	for index,key in ipairs(sorted_index(ArmorCraft_Formulas)) do
		list_of_formulas[key] = key

		local okey = 'formula' .. index
		list_of_formula_keys[okey] = key

		-- Initialise the formula edit section
		options.args.formulas.args[okey] = {}
		options.args.formulas.args[okey].name			= key
		options.args.formulas.args[okey].type 			= "input"
		options.args.formulas.args[okey].desc 			= "Description for formula " .. key
		options.args.formulas.args[okey].order 		= index +10
		options.args.formulas.args[okey].multiline 	= true
		options.args.formulas.args[okey].width 		= 'full'
	end

	return options
end


-------------------------------------------------
-- Initialisation functions
-------------------------------------------------

function ArmorCraft:InitOptions()

	local config = LibStub("AceConfig-3.0")
	local dialog = LibStub("AceConfigDialog-3.0")
	local options = createconfig()

	-- Ace3 dialog setup
	config:RegisterOptionsTable('ArmorCraft', options)
	dialog:SetDefaultSize("ArmorCraft", 700, 500)

	-- Blizzard dialog setup
	config:RegisterOptionsTable('ArmorCraft-Bliz', options.args.main)
	dialog:SetDefaultSize("ArmorCraft-Bliz", 600, 400)
	dialog:AddToBlizOptions("ArmorCraft-Bliz", "ArmorCraft")


	config:RegisterOptionsTable('ArmorCraft-Formulas to Use', options.args.formulas_to_use)
	config:RegisterOptionsTable('ArmorCraft-Edit Formula', options.args.formulas)

	-- Added in reverse so they appear in the proper order in the Blizzard dialog
	dialog:AddToBlizOptions('ArmorCraft-Formulas to Use', options.args.formulas_to_use.name, 'ArmorCraft')
	dialog:AddToBlizOptions('ArmorCraft-Edit Formula', options.args.formulas.name, 'ArmorCraft')

end

-------------------------------------------------
-- Display the option dialog
-------------------------------------------------

function ArmorCraft:DisplayOptions()
	--print("Displaying config dialog")

	LibStub("AceConfigDialog-3.0"):Open('ArmorCraft')
end
